package com.lastcrdg.loveview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * author：liluo
 * e-mail：lastcrazysun@163.com
 * company：cusc
 * date：2017/10/10 9:38
 * description：
 */

public class LoveView extends View {

    private static final float C = 0.551915024494f;
    private int centerX;
    private int centerY;
    private float radius = 300f;
    private float[] helpPotints = new float[16]; //从X轴顺时针开始
    private float[] dataPotints = new float[8];

    private Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint helpLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint linePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    private Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public LoveView(Context context) {
        super(context);
    }

    public LoveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public LoveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initPaint() {
        circlePaint.setColor(Color.GRAY);
        circlePaint.setStyle(Paint.Style.FILL);
        helpLinePaint.setColor(Color.GRAY);
        helpLinePaint.setStyle(Paint.Style.STROKE);
        helpLinePaint.setStrokeWidth(4);
        linePaint.setColor(Color.RED);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeWidth(8);
        textPaint.setColor(Color.RED);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setStyle(Paint.Style.FILL_AND_STROKE);
        textPaint.setTextSize(60);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        centerX = w / 2;
        centerY = h / 2;
        //初始化坐标
        initCoordinate();
    }

    private void initCoordinate() {
        //初始化数据点
        dataPotints[0] = radius;
        dataPotints[1] = 0;
        dataPotints[2] = 0;
        dataPotints[3] = radius;
        dataPotints[4] = -radius;
        dataPotints[5] = 0;
        dataPotints[6] = 0;
        dataPotints[7] = -radius;
        //初始化辅助点
        float helpC = radius * C;
        helpPotints[0] = radius;
        helpPotints[1] = helpC;
        helpPotints[2] = helpC;
        helpPotints[3] = radius;
        helpPotints[4] = -helpC;
        helpPotints[5] = radius;
        helpPotints[6] = -radius;
        helpPotints[7] = helpC;
        helpPotints[8] = -radius;
        helpPotints[9] = -helpC;
        helpPotints[10] = -helpC;
        helpPotints[11] = -radius;
        helpPotints[12] = helpC;
        helpPotints[13] = -radius;
        helpPotints[14] = radius;
        helpPotints[15] = -helpC;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(centerX, centerY);
//        drawPointAndLines(canvas);
        drawLove(canvas);
        if (current == count) {
            if (currentText < 6) {
                currentText++;
                float[] posL = {-120, -100, -120, 0, -120, 100};
                float[] posR = {120, -100, 120, 0, 120, 100};
                if (currentText < 3) {
                    canvas.drawPosText("XXX".toCharArray(), 0, currentText, posL, textPaint);
                } else {
                    canvas.drawPosText("XXX".toCharArray(), 0, 3, posL, textPaint);
                    canvas.drawPosText("XXX".toCharArray(), 0, currentText - 3, posR, textPaint);
                }
                postInvalidateDelayed(duration / count);
            }
        }
        if (current < count) {
            current++;
            dataPotints[7] -= -200 / count;
            helpPotints[3] -= 150 / count;
            helpPotints[5] -= 150 / count;
            helpPotints[0] -= 25 / count;
            helpPotints[6] -= -25 / count;
            postInvalidateDelayed(duration / count);
        }
    }

    private int duration = 3000;
    private int current = 1;
    private int currentText = 0;
    private int count = 10;

    private void drawLove(Canvas canvas) {
        Path path = new Path();
        for (int i = 0; i < dataPotints.length / 2; i++) {
            path.moveTo(dataPotints[i * 2], dataPotints[i * 2 + 1]);
            if (i == 3) {
                path.cubicTo(helpPotints[i * 4], helpPotints[i * 4 + 1], helpPotints[i * 4 + 2], helpPotints[i * 4 + 3],
                        dataPotints[0], dataPotints[1]);
            } else {
                path.cubicTo(helpPotints[i * 4], helpPotints[i * 4 + 1], helpPotints[i * 4 + 2], helpPotints[i * 4 + 3],
                        dataPotints[i * 2 + 2], dataPotints[i * 2 + 3]);
            }
        }
        canvas.drawPath(path, linePaint);
    }

    private void drawPointAndLines(Canvas canvas) {
        //画点
        for (int i = 0; i < dataPotints.length / 2; i++) {
            canvas.drawCircle(dataPotints[i * 2], dataPotints[i * 2 + 1], 10, circlePaint);
        }
        for (int i = 0; i < helpPotints.length / 2; i++) {
            canvas.drawCircle(helpPotints[i * 2], helpPotints[i * 2 + 1], 10, circlePaint);
        }
        //画线
        canvas.drawLine(dataPotints[0], dataPotints[1], helpPotints[0], helpPotints[1], helpLinePaint);
        canvas.drawLine(dataPotints[2], dataPotints[3], helpPotints[2], helpPotints[3], helpLinePaint);
        canvas.drawLine(dataPotints[2], dataPotints[3], helpPotints[4], helpPotints[5], helpLinePaint);
        canvas.drawLine(dataPotints[4], dataPotints[5], helpPotints[6], helpPotints[7], helpLinePaint);
        canvas.drawLine(dataPotints[4], dataPotints[5], helpPotints[8], helpPotints[9], helpLinePaint);
        canvas.drawLine(dataPotints[6], dataPotints[7], helpPotints[10], helpPotints[11], helpLinePaint);
        canvas.drawLine(dataPotints[6], dataPotints[7], helpPotints[12], helpPotints[13], helpLinePaint);
        canvas.drawLine(dataPotints[0], dataPotints[1], helpPotints[14], helpPotints[15], helpLinePaint);
    }
}
